<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use App\Models\yb\ActivityModel;
use App\Models\yb\CouponModel;
use App\Models\yb\GoodsContentModel;
use App\Models\yb\GoodsModel;
use App\Models\yb\GoodsOrderModel;
use App\Models\yb\NetworkGoodsModel;
use App\Models\yb\UserModel;
use App\Models\yb\NetworkPointsModel;
use App\Models\yb\WaterTicketModel;
use Encore\Admin\Layout\Content;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;


class HomeController extends Controller
{

    protected $header = '主页';

    public function index()
    {

        return Admin::content(function (Content $content) {

            $date = $this->to_sex_month();

            $content->header($this->header);
            $content->description('控制台');
            if(admin::user()->type ==2){

                //柱状图数据
                $user_data = array();
                for($i=0;$i<count($date);$i++){
                    $amount_data[$i] = GoodsOrderModel::where(['status'=>4,'network_id'=>admin::user()->id])->where('created_at','like','%'.$date[$i].'%')->sum('amount');
                }

                //上方统计数据
                //商品数
                $data['goods_count'] = NetworkGoodsModel::where(['network_id'=>admin::user()->id])->count();
                //销售额
                $data['sales_volume'] = admin::user()->sales_volume;
                //订单数
                $data['order_count'] = GoodsOrderModel::where(['network_id'=>admin::user()->id])->count();

                $network_goods_data = NetworkGoodsModel::where(['network_id'=>admin::user()->id])->orderBy('goods_sales','desc')->select(['goods_id','goods_sales'])->skip(0)->take(10)->get();

                foreach($network_goods_data as $key=>$val){

                    $goods_data = GoodsModel::where(['id'=>$val['goods_id'],'network_id'=>admin::user()->id])->select(['goods_title'])->first();;
                    if(!empty($goods_data)){
                        if(mb_strlen($goods_data['goods_title'])>8){
                            $goods_data['goods_title'] = mb_substr($goods_data['goods_title'],0,7)."...";
                        }
                        $network_goods_data[$key]['goods_title'] = $goods_data['goods_title'];
                    }else{
                        unset($network_goods_data[$key]);
                    }

                }

                $content->body(view('admin.network_home',[
                    'date'=>json_encode($date),
                    'amount_data'=>json_encode($amount_data),
                    'user_data'=>json_encode($user_data),
                    'data'=>$data,
                    'goods_data'=>$network_goods_data,
                ]));
            }else{

                //柱状图数据
                $amount_data = array();
                $user_data = array();
                for($i=0;$i<count($date);$i++){
                    $amount_data[$i] = GoodsOrderModel::where(['status'=>4])->where('created_at','like','%'.$date[$i].'%')->sum('amount');
                    $user_data[$i] = UserModel::where('created_at','like','%'.$date[$i].'%')->count();
                }

                //上方统计数据
                //注册人数
                $data['user_count'] = UserModel::count();
                //待审核评论数
                $data['content_count'] = GoodsContentModel::where(['status'=>0])->count();
                //网点数
                $data['amdin_count'] = AdminUserModel::where(['type'=>2])->count();
                //商品数
                $data['goods_count'] = GoodsModel::count();
                //水票数
                $data['water_ticket_count'] = WaterTicketModel::count();
                //活动数
                $data['activity_count'] = ActivityModel::count();
                //优惠券数
                $data['coupon_count'] = CouponModel::count();
                //销售额
                $data['order_amount_count'] = GoodsOrderModel::where(['status'=>4])->sum('amount');
                //订单数
                $data['order_count'] = GoodsOrderModel::count();

                $goods_data = GoodsModel::orderBy('goods_sales','desc')->select(['goods_title','goods_sales'])->skip(0)->take(10)->get();
                foreach($goods_data as $key=>$val){
                    if(mb_strlen($val['goods_title'])>8){
                        $goods_data[$key]['goods_title'] = mb_substr($val['goods_title'],0,7)."...";
                    }
                }

                $network_data =AdminUserModel::where(['type'=>2])->select(['name','sales_volume'])->orderBy('sales_volume','desc')->skip(0)->take(10)->get();
                foreach($network_data as $key=>$val){
                    if(mb_strlen($val['name'])>8){
                        $network_data[$key]['name'] = mb_substr($val['name'],0,7)."...";
                    }
                }

                $content->body(view('admin.home',[
                    'date'=>json_encode($date),
                    'amount_data'=>json_encode($amount_data),
                    'user_data'=>json_encode($user_data),
                    'data'=>$data,
                    'goods_data'=>$goods_data,
                    'network_data'=>$network_data
                ]));
            }
        });

    }

    //配送区域划分
    public function RegionalDivision(Request $request)
    {
        $type = $request->get('type','1');

        return Admin::content(function (Content $content)use($type) {


            $content->header($this->header);
            $content->description('控制台');
            $content->title('网点配送区域划分');
            $admin_list = AdminUserModel::where(['type'=>2])->select(['id','name'])->get();

            $admin_list = $admin_list->toArray();

            if($type == 2){

                $content->body(view('admin.home2',['admin_list'=>json_encode($admin_list)]));
            }else{

                $content->body(view('admin.home5'));

                // $list = array();
                // $data = NetworkPointsModel::select(['id','admin_id','points','created_at'])->get();
                // foreach($data as $key=>$val){
                //     $admin_data = AdminUserModel::where(['id'=>$val['admin_id']])->select(['id','name','latitude','longitude'])->first();
                //     $points = json_decode($val['points'],true);
                //     $areapoints = "";
                //     foreach ($points as $k=>$v){
                //         $areapoints.=",".$v['latLng']['lat']."_".$v['latLng']['lng'];
                //     }
                //     if(!empty($areapoints)){
                //         $areapoints = mb_substr($areapoints,1);
                //     }

                //     $list[$key]['id'] = $admin_data['id'];
                //     $list[$key]['remark'] = $admin_data['name'];
                //     $list[$key]['name'] = $admin_data['name'];
                //     $list[$key]['centerpoint'] = $admin_data['longitude'].",".$admin_data['latitude'];
                //     $list[$key]['operdate'] = strtotime($val['created_at']);
                //     $list[$key]['areapoints'] = $areapoints;
                // }

                // $content->body(view('admin.home3',['areapoints'=>json_encode($list),'admin_list'=>json_encode($admin_list)]));

            }
        });

    }

    public function RegionalDivisions(Request $request){

        $type = $request->get('type','1');

        header('refresh:1; url=/admin/regional/division?type'.$type);

    }

    public  function to_sex_month(){
        $today = date("Y-m-d");
        $arr = array();
        $old_time = strtotime('-12 month',strtotime($today));
        for($i = 0;$i <= 12; ++$i){
            $t = strtotime("+$i month",$old_time);
            $arr[]=date('Y-m',$t);
        }
        return $arr;
    }


    public function admin_points(Request $request){

        $points = $request->post('points');
        if(empty($points)){
            return response()->json(['code'=>'201','msg' => '请框选网点区域']);
        }

        $admin_id = $request->post('admin_id');
        if(empty($admin_id)){
            return response()->json(['code'=>'201','msg' => '请选择网点']);
        }

        NetworkPointsModel::insert([
            'admin_id'=>$admin_id,
            'points'=>$points,
            'created_at'=> date('Y-m-d H:i:s',time())
        ]);

        return response()->json(['code'=>'200','msg' => '设置成功']);

    }

    public function admin_points_delete(Request $request){

        $admin_id = $request->post('admin_id');
        if(empty($admin_id)){
            return response()->json(['code'=>'201','msg' => '请选择网点']);
        }

        NetworkPointsModel::where(['admin_id'=>$admin_id])->delete();

        return response()->json(['code'=>'200','msg' => '设置成功']);

    }

}



